/**
 *
 */
package br.com.ac.fatureweb.view;

import java.util.Arrays;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import br.com.ac.fatureweb.integration.enums.EnumSituacaoLigacao;
import br.com.ac.fatureweb.model.billing.Produto;
import br.com.ac.fatureweb.model.carga.Imovel;
import br.com.ac.fatureweb.service.NgcCadastroBean;
import br.com.ac.fatureweb.utilitary.CrudDefaultBean;

/**
 * @author aldivone
 *
 */
@Named
@ViewScoped
public class CadastroBean extends CrudDefaultBean<Imovel, NgcCadastroBean> {

	private static final long serialVersionUID = -5343575950617308132L;

	@Inject
	private NgcCadastroBean ngcCadastroBean;

	@PostConstruct
	public void inicialize() {
		setService(ngcCadastroBean);
	}

	public CadastroBean() {
		super();
	}

	@Override
	public Imovel getParametersSearch() {
		Imovel imovel = super.getParametersSearch();

		if (imovel != null && imovel.getCompetencia() != null
				&& !imovel.getCompetencia().trim().isEmpty()) {
			imovel.setMes(Integer.parseInt(imovel.getCompetencia().substring(0,
					2)));
			imovel.setAno(Integer.parseInt(imovel.getCompetencia().substring(3,
					7)));
		}
		return imovel;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @author Brunno Gonçalves (brunnogoncalves@gmail.com) 29/01/2015
	 * 
	 * @see
	 * br.com.ac.fatureweb.utilitary.CrudDefaultBean#runAfterCallingService()
	 */
	@Override
	protected void runAfterCallingService() {
		super.runAfterCallingService();

		getEntity().setTarifaAux(
				ngcCadastroBean.getTarifa(getEntity().getTarifa(), getEntity()
						.getCarga().getId()));

		getEntity().setLstDebitos(
				ngcCadastroBean.getLstDebitos(getEntity().getInscricao(),
						getEntity().getCarga().getId()));
		getEntity().setHistorico(
				ngcCadastroBean.getHistorico(getEntity().getInscricao(),
						getEntity().getCarga().getId()));
		getEntity().setLstLancamentoFuturo(
				ngcCadastroBean.getLstLancamentoFuturo(getEntity()
						.getInscricao(), getEntity().getCarga().getId()));

		getEntity().setLstSaldoDebitoCredito(
				ngcCadastroBean.getLstSaldoDebitoCredito(getEntity()
						.getInscricao(), getEntity().getCarga().getId()));

		getEntity().setLstTarifaSocial(
				ngcCadastroBean.getLstTarifaSocial(getEntity().getInscricao(),
						getEntity().getCarga().getId()));
		getEntity().setLstEconomias(
				ngcCadastroBean.getLstEconomia(getEntity().getInscricao(),
						getEntity().getCarga().getId()));

		/*
		 * Retorno
		 */
		getEntity().setConta(
				ngcCadastroBean.getConta(getEntity().getInscricao(),
						getEntity().getCarga().getId()));

		getEntity().setLstLancamentoCobrado(
				ngcCadastroBean.getLstLancamentoCobrado(getEntity()
						.getInscricao(), getEntity().getCarga().getId()));

		getEntity().setLogLeitura(
				ngcCadastroBean.getLogLeitura(getEntity().getInscricao(),
						getEntity().getCarga().getId()));

		getEntity().setLstProduto(
				ngcCadastroBean.getLstProduto(getEntity().getInscricao(),
						getEntity().getCarga().getId()));

		if (getEntity().getLstProduto() != null
				&& !getEntity().getLstProduto().isEmpty()) {
			for (Produto produto : getEntity().getLstProduto()) {
				produto.setLstValor(ngcCadastroBean.getLstValor(getEntity()
						.getInscricao(), getEntity().getCarga().getId(),
						produto.getCodigo()));

				produto.setVolume(ngcCadastroBean.getVolume(getEntity()
						.getInscricao(), getEntity().getCarga().getId(),
						produto.getCodigo()));
			}
		}

	}

	public List<EnumSituacaoLigacao> getLstEnumSituacaoLigacao() {
		return Arrays.asList(EnumSituacaoLigacao.values());
	}

}
